﻿using System;

namespace ExtremeML.Core
{
	/// <summary>
	/// Exposes shared static utility methods.
	/// </summary>
	internal static class Utility
	{
        /// <summary>
        /// Safely formats the passed string with the passed arguments, ignoring null or empty arrays.
        /// </summary>
        /// <param name="format">A template <see cref="string"/> into which the passed atguments will be inserted.</param>
        /// <param name="args">A parameter array of <see cref="object"/> that contains values to insert into the template.</param>
        /// <returns>The passed template string populated with the passed argument values.</returns>
        internal static string SafeFormat(
			string format,
			params object[] args)
		{
			if (string.IsNullOrEmpty(format)) return string.Empty;
			if (args == null || args.Length == 0) return format;
			return string.Format(format, args);
		}

        /// <summary>
        /// Verifies that the specified member of the passed parameter array is of the specified type.
        /// </summary>
        /// <param name="index">The zero-based index of the parameter array member to be validated.</param>
        /// <param name="type">The <see cref="Type"/> expected at the specified index.</param>
        /// <param name="args">The parameter array of <see cref="object"/> to be validated.</param>
        /// <remarks>
        /// This method does not allow the specified argument to be null.
        /// </remarks>
        internal static void ValidateParamsArgument(
            int index,
            Type type,
            params object[] args)
        {
            if (args == null) throw new ArgumentNullException("args");
            if (args.Length <= index) throw new ArgumentOutOfRangeException("index");
            if (args[index] == null) throw new ArgumentException(string.Format("Argument {0} is null.", index));
            if (!type.IsAssignableFrom(args[index].GetType())) throw new ArgumentException(string.Format("Argument {0} is not of type {1}.", index, type.Name));
        }

		/// <summary>
		/// Converts the passed graphical dimension from points to pixels
		/// </summary>
		/// <param name="points">A <see cref="double"/> that represents a dimension in points.</param>
		/// <returns>An <see cref="int"/> that represents the value converted to pixels.</returns>
		internal static int PointsToPixels(
			double points)
		{
//ToDo: review whether there is a better way than simply assuming resolution of 96dpi (as per OpenXML Specification)
			return (int) points * 96 / 72;
		}
	}
}